import com.code.learn.Ipersistance.dao.IUserDao;
import com.code.learn.Ipersistance.io.Resources;
import com.code.learn.Ipersistance.pojo.User;
import com.code.learn.Ipersistance.sqlSession.SqlSession;
import com.code.learn.Ipersistance.sqlSession.SqlSessionFactory;
import com.code.learn.Ipersistance.sqlSession.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.*;
import java.util.List;

/**
 * @author Nina
 * @create 2020/12/14 下午11:23
 */
public class IpersistenceTest {

    SqlSession sqlSession = null;

    /**
     * 单元测试之前增强，所有的@Test修饰的方法都会先调用该方法
     * @throws Exception
     */
    @Before
    public void before() throws Exception{
        // 读取硬盘上sqlMapConfig.xml配置文件以字节输入流的形式存储在内存中
//        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig_mapper.xml");
        // 构建者模式构建SqlSessionFactory，同时读取字节输入流将配置信息存储到Configuration中
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        // 获取SqlSession，也就是DefaultSqlSessionFactory.openSqlSession()
        sqlSession = sqlSessionFactory.openSqlSession();
    }

    /**
     * 传统调用模式查询
     * 测试查询返回集合
     * @throws Exception
     */
    @Test
    public void selectAllTest() throws Exception {
        List<User> users = sqlSession.selectList("user.selectList");
        // 这里User类中加入了toString()，方便输出查看user数据
        for (User user : users) {
            System.out.println(user);
        }
    }

    /**
     * 传统调用模式查询
     * 测试查询单条记录
     * @throws Exception
     */
    @Test
    public void selectOneTest() throws Exception {
        User user1 = new User();
        user1.setId(1);
        // TODO 注意:目前的代码不知道单独传字符串这种参数，因为反射的时候是按照class.property的形式做的
//        User user = (User) sqlSession.selectOne("user.selectOne","张三");
        User user = (User) sqlSession.selectOne("user.selectOne",user1);
        System.out.println(user);
    }

    /**
     * mapper()方法调用查询
     * 前置： 需要读取sqlMapConfig_mapper.xml，这样UserMapper.xml符合mapper()方法调用的要求
     * @throws Exception
     */
    @Test
    public void selectMapperTest() throws Exception {
        IUserDao userDao = sqlSession.getMapper(IUserDao.class);
        List<User> all = userDao.findAll();
        for (User user : all) {
            System.out.println(user);
        }

        User user = new User();
        user.setId(1);
        User record = userDao.findByCondition(user);
        System.out.println(record);
    }

    @Test
    public void insertMapperTest() throws Exception {
        IUserDao userDao = sqlSession.getMapper(IUserDao.class);
        User user = new User();
        user.setId(2);
        user.setName("test");
       userDao.add(user);
    }

    @Test
    public void updateMapperTest() throws Exception {
        IUserDao userDao = sqlSession.getMapper(IUserDao.class);
        User user = new User();
        user.setId(2);
        user.setName("李四");
        userDao.modify(user);
    }

    @Test
    public void deleteMapperTest() throws Exception {
        IUserDao userDao = sqlSession.getMapper(IUserDao.class);
        User user = new User();
        user.setId(2);
        userDao.remove(user);
    }
}
